package com.deanlib.cabinet.utils;

import com.deanlib.cabinet.app.Constants;
import com.deanlib.cabinet.data.model.Key;
import com.tencent.mmkv.MMKV;

import java.util.Objects;
import java.util.Set;

import retrofit2.http.PUT;

/**
 * 配置管理
 * Created by Dean on 2022/7/4
 */
public class ConfigManager {

    public static final String KEY_OSS_KEY = "KEY_OSS_KEY";
    public static final String KEY_SYNC_DIR = "KEY_SYNC_DIR";
    public static final String KEY_SYNC_IMAGE = "KEY_SYNC_IMAGE";
    public static final String KEY_SYNC_VIDEO = "KEY_SYNC_VIDEO";

    private static ConfigManager configManager;

    boolean isTestSync = true; //todo 测试时使用的同步开关

    public ConfigManager(){

    }

    public static ConfigManager getInstance(){
        if (configManager == null){
            synchronized (ConfigManager.class){
                if (configManager == null){
                    configManager = new ConfigManager();
                }
            }
        }
        return configManager;
    }

    /**
     * 暂时方法，开发调试用
     * @return
     */
    public boolean isAllowUpload(){
        return isTestSync;//todo 压缩算法变了，需要重新写压缩
    }

    /**
     * 是否开启同步功能
     * @return
     */
    public boolean enableSync(){
        return isTestSync;//todo
    }

    /**
     * 是否开启共享功能
     * @return
     */
    public boolean enableShared(){
        return false;//todo
    }

    public void setOssKey(Key key){
        MMKV.defaultMMKV().encode(ConfigManager.KEY_OSS_KEY, key);
    }
    /**
     * 上传需要的Key
     * @return
     */
    public Key getOssKey(){
        return MMKV.defaultMMKV().decodeParcelable(KEY_OSS_KEY, Key.class, null);
    }

    /**
     * 需要同步的文件目录
     * @return
     */
    public Set<String> getSyncDirList(){
        return MMKV.defaultMMKV().getStringSet(KEY_SYNC_DIR, Set.of(
//                "/storage/emulated/0/DCIM/Camera/",
                "/storage/emulated/0/Download/test"
        ));//todo
    }

    /**
     * 是否同步图片资源
     * @return
     */
    public boolean isSyncImageResource(){
        return MMKV.defaultMMKV().getBoolean(KEY_SYNC_IMAGE , isTestSync);
    }

    /**
     * 是否同步视频资源
     * @return
     */
    public boolean isSyncVideoResource(){
        return MMKV.defaultMMKV().getBoolean(KEY_SYNC_VIDEO , isTestSync);
    }

    /**
     * 原始资源备份服务地址
     * @return
     */
    public String getOriginalResourceBackupServiceUrl(){
        return "";
    }

    /**
     * 需要压缩
     * @return
     */
    public boolean needCompress(){
        return true;
    }

    /**
     * 图片最小边上限
     * @return
     */
    public int getCompressImageLimitDimen(){
        return 1440;
    }

    /**
     * 视频最小边上限
     * @return
     */
    public int getCompressVideoLimitDimen(){
        return 1080;
    }

    /**
     * 帧数
     * @return
     */
    public int getCompressFrameRate(){
        return 30;
    }

    /**
     * 视频压缩码率
     *
     * 偏移量范围 0.25 - 0.35  值越大越清晰
     * @return 单位 bps
     */
    public long getCompressVideoBitrate(int width, int height, int frameRate){
        //对于720p(1280x720)与1080p(1920x1080) 清晰度可以接受
        // 而且 对于微信的小视频 720p 的压缩数据吻合的很好，所以具有普适性
        return (long)(0.25 * (Math.pow(width * height * frameRate, 0.56) * 1000));
    }
}
